/**
 * Created by XianTao on 2016/5/28
 * <p/>
 * Copyright (c) 2015-2015
 * Apache License
 * Version 2.0, January 2004
 * http://www.apache.org/licenses/
 */
package org.lego.captcha.restful;

import com.google.code.kaptcha.servlet.KaptchaExtend;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class KaptchaController extends KaptchaExtend {
    @RequestMapping(value = "/lego/captcha.jpg", method = RequestMethod.GET)
    public void loginCaptcha(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String time = req.getParameter("time");
        if (ObjectUtils.isEmpty(time)) {
            throw new RuntimeException("bad request1 for /lego/captcha.jpg");
        }
//        try {
//            long requestTime = Long.parseLong(time);
//            if (System.currentTimeMillis() - requestTime > 1000 || System.currentTimeMillis() - requestTime < 0) {
//                throw new RuntimeException("bad request2 for /lego/captcha.jpg");
//            }
//        } catch (NumberFormatException e) {
//            throw new RuntimeException("bad request3 for /lego/captcha.jpg");
//        }
        super.captcha(req, resp);
    }
}
